package cn.dengta.webapp.ai.model;

import java.util.*;
import java.util.stream.Collectors;

import cn.dengta.common.model.ValueProxy;
import cn.dengta.common.util.Common;
import cn.dengta.context.model.*;
import cn.dengta.context.rpc.CachedConfig;
import cn.dengta.context.web.ImageConverter;
import cn.dengta.webapp.im.dto.ImCommonConfig;
import cn.dengta.webapp.user.model.Gender;
import lombok.Getter;
import me.codeplayer.util.X;

import static cn.dengta.context.model.ImEnumI18nKey.*;

@Getter
public enum AIGCVoiceId implements LabelI18nProxy<AIGCVoiceId, String> {
	MALE_QN_QINGSE("male-qn-qingse", AIGC_MALE_QN_QINGSE, Gender.MALE),
	MALE_QN_JINGYING("male-qn-jingying", AIGC_MALE_QN_JINGYING, Gender.MALE),
	MALE_QN_BADAO("male-qn-badao", AIGC_MALE_QN_BADAO, Gender.MALE),
	MALE_QN_DAXUESHENG("male-qn-daxuesheng", AIGC_MALE_QN_DAXUESHENG, Gender.MALE),
	PRESENTER_MALE("presenter_male", AIGC_PRESENTER_MALE, Gender.MALE),
	AUDIOBOOK_MALE_1("audiobook_male_1", AIGC_AUDIOBOOK_MALE_1, Gender.MALE),

	FEMALE_SHAONV("female-shaonv", AIGC_FEMALE_SHAONV, Gender.FEMALE),
	FEMALE_YUJIE("female-yujie", AIGC_FEMALE_YUJIE, Gender.FEMALE),
	FEMALE_CHENGSHU("female-chengshu", AIGC_FEMALE_CHENGSHU, Gender.FEMALE),
	FEMALE_TIANMEI("female-tianmei", AIGC_FEMALE_TIANMEI, Gender.FEMALE),
	PRESENTER_FEMALE("presenter_female", AIGC_PRESENTER_FEMALE, Gender.FEMALE),
	AUDIOBOOK_FEMALE_1("audiobook_female_1", AIGC_AUDIOBOOK_FEMALE_1, Gender.FEMALE),
	;

	public static final String ossDir = "ai/voice/";

	final ValueProxy<AIGCVoiceId, String> proxy;

	final Gender gender;

	final String voicePath;

	AIGCVoiceId(String value, String label, Gender gender) {
		this.gender = gender;
		voicePath = "/" + ossDir + value;
		proxy = new ValueProxy<>(this, value, label);
	}

	public String getVoicePath() {
		return voicePath + ".mp3";
	}

	public static String getVoiceTextUrl(WesKit wesKit, String voice) {
		return ImageConverter.getCdnDomain(wesKit.isInterior()) + voice;
	}

	public static AIGCVoiceId of(String value) {
		return MALE_QN_QINGSE.getValueOf(value);
	}

	public AIGCVoice getVoice(String text) {
		AIGCVoice voice = new AIGCVoice();
		voice.setVoiceId(this.getValue());
		voice.setText(text);
		voice.setStdAuditionUrl(getVoiceTextUrl(WesKit.main, getVoicePath()));
		return voice;
	}

	public AIGCVoice getVoice() {
		final CachedConfig<ImCommonConfig> cache = ImCommonConfig.cache();
		final ImCommonConfig config = cache.get();
		AIGCVoice voice = new AIGCVoice();
		voice.setVoiceId(this.getValue());
		voice.setText(config.getAiConfig().findAuditionText());
		return voice;
	}

	public static Map<Gender, Option<List<AIGCVoice>>> getVoices() {
		final CachedConfig<ImCommonConfig> cache = ImCommonConfig.cache();
		final ImCommonConfig config = cache.get();
		final Map<Gender, List<AIGCVoiceId>> collect = Arrays.stream(AIGCVoiceId.values()).collect(Collectors.groupingBy(AIGCVoiceId::getGender));
		collect.forEach((k, v) -> X.castType(Common.replaceListValues(v, e -> new Option<>(e.getVoice(config.getAiConfig().findAuditionText()), e.getLabel()))));
		return X.castType(collect);
	}

	@Override
	public ValueProxy getProxy() {
		return proxy;
	}
}
